package com.attendance.controller;

import com.attendance.pojo.Department;
import com.attendance.result.LayuiMiniPage;
import com.attendance.service.DepartmentServiceImpl;
import com.attendance.web.Result;
import com.attendance.web.ResultCode;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.security.RolesAllowed;

/**
 * 部门控制器
 *
 * @author 今朝
 * @date 2021/03/17
 */
@Controller
@RequestMapping("/system/depart")
@RolesAllowed("ADMIN")
public class DepartmentController {
    @Autowired
    DepartmentServiceImpl departmentService;

    @GetMapping()
    public String table() {
        return "/system/depart/table";
    }

    @GetMapping("/edit")
    public String edit() {
        return "/system/depart/edit";
    }

    @GetMapping("/add")
    public String add() {
        return "/system/depart/add";
    }

    @GetMapping("/edit/{id}")
    public ModelAndView edit(@PathVariable("id") Long id) {
        ModelAndView modelAndView = new ModelAndView("/system/depart/edit");
        Department department = departmentService.selectById(id);
        modelAndView.addObject("department", department);
        Department parent = departmentService.selectById(department.getParentId());
        if (parent != null) {
            modelAndView.addObject("parentName", parent.getName());
        } else {
            modelAndView.addObject("parentName", "根节点");
        }
        return modelAndView;
    }

    @GetMapping("/list")
    @ResponseBody
    public Result getList() {
        return new Result(ResultCode.SUCCESS, departmentService.getList());
    }

    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public Result delete(@PathVariable Long id) {
        try {
            if (departmentService.delete(id)) {
                return new Result(ResultCode.SUCCESS);
            } else {
                return new Result(ResultCode.FAIL);
            }
        } catch (Exception e) {
            return new Result(502, e.getMessage(), false);
        }
    }

    @GetMapping("/binding/{ids}")
    public ModelAndView binding(@PathVariable String ids) {
        ModelAndView modelAndView = new ModelAndView("/system/depart/binding");
        modelAndView.addObject("uIds", ids);
        return modelAndView;
    }

    @ResponseBody
    @GetMapping("/listPage")
    public LayuiMiniPage<Department> getListPage(Integer page, Integer limit, String name) {
        if (name != null) {
            Page<Department> listPage = departmentService.getListPage(page, limit, name);
            return new LayuiMiniPage<>(200, "查询成功", listPage.getTotal(), listPage.getRecords());
        }
        Page<Department> listPage = departmentService.getListPage(page, limit);
        return new LayuiMiniPage<>(200, "查询成功", listPage.getTotal(), listPage.getRecords());
    }

    @ResponseBody
    @PostMapping("/update")
    public Result update(Department department) {
        if (departmentService.updateById(department)) {
            return new Result(ResultCode.SUCCESS);
        } else {
            return new Result(ResultCode.FAIL);
        }
    }

    @ResponseBody
    @PutMapping("/add")
    public Result add(Department department) {
        try {
            if (departmentService.add(department)) {
                return new Result(ResultCode.SUCCESS);
            } else {
                return new Result(ResultCode.FAIL);
            }
        } catch (Exception e) {
            return new Result(502, e.getMessage(), false);
        }
    }

    @ResponseBody
    @GetMapping("/departTree")
    public Result getRoots() {
        return new Result(ResultCode.SUCCESS, departmentService.getRoots());
    }

    @ResponseBody
    @PostMapping("/binding")
    public Result binding(String uIds, Long dId) {
        if (departmentService.binding(uIds, dId)) {
            return new Result(ResultCode.SUCCESS);
        } else {
            return new Result(ResultCode.FAIL);
        }
    }
}
